package com.block.common.enums;

import com.block.common.constant.AppRspCodeConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * 通过枚举初始化返回信息的map
 */
public enum AppRspCodeEnum {
    /**
     * 系统相关
     **/
    SUCCESS(AppRspCodeConstant.SUCCESS, "成功"),
    PARAM_ERROR(AppRspCodeConstant.PARAM_ERROR, "参数错误：%s"),
    SYS_EXCEPTION(AppRspCodeConstant.SYS_EXCEPTION, "系统繁忙，请稍后重试"),
    SIGN_ERROR(AppRspCodeConstant.SIGN_ERROR, "请求数据签名失败"),
    CHECK_SIGN_ERROR(AppRspCodeConstant.CHECK_SIGN_ERROR, "验证签名失败"),
    TOKEN_ISNULL(AppRspCodeConstant.TOKEN_ISNULL, "token为空或已失效,请重新登录"),
    SEARCH_RESULT_EMPTY(AppRspCodeConstant.SEARCH_RESULT_EMPTY, "搜索结果为空"),
    ACTION_ERROR(AppRspCodeConstant.ACTION_ERROR, "操作失败，请重试"),
    REQ_MSG_NULL(AppRspCodeConstant.REQ_MSG_NULL, "请求数据为空"),
    LIMIT_REQUEST_OFTEN_REQUEST(AppRspCodeConstant.LIMIT_REQUEST_OFTEN_REQUEST, "访问过于频繁,请稍后再试!"),
    LIMIT_REQUEST_NO_AUTH(AppRspCodeConstant.LIMIT_REQUEST_NO_AUTH, "您无权限访问此资源,请联系管理员"),
    LIMIT_REQUEST_TOMORROW_AGAIN(AppRspCodeConstant.LIMIT_REQUEST_TOMORROW_AGAIN, "异常访问,请明天再试!"),
    VERIFY_CODE_ERROR(AppRspCodeConstant.VERIFY_CODE_ERROR, "验证码错误"),
    PASSWORD_ERROR(AppRspCodeConstant.PASSWORD_ERROR, "登录密码错误"),
    LOGIN_ERROR_AGAIN(AppRspCodeConstant.LOGIN_ERROR_AGAIN, "登录失败，请重试"),
    APPID_INFO_ERROR(AppRspCodeConstant.APPID_INFO_ERROR, "appid错误"),
    REQUEST_HEADER_LANG_NULL(AppRspCodeConstant.REQUEST_HEADER_LANG_NULL, "请求头语言为空"),
    REQUEST_HEADER_TIMESTAMP_NULL(AppRspCodeConstant.REQUEST_HEADER_TIMESTAMP_NULL, "请求头时间戳为空"),
    REQUEST_HEADER_MOBILE_INFO_NULL(AppRspCodeConstant.REQUEST_HEADER_MOBILE_INFO_NULL, "请求头手机信息为空"),
    DATA_INFO_NULL(AppRspCodeConstant.DATA_INFO_NULL, "数据为空"),

    /**
     * 用户相关
     */
    USER_NOT_EXISTS(AppRspCodeConstant.USER_NOT_EXISTS,"用户信息不存在"),

    /**
     * 短信 校验码
     */
    AREA_CODE_CONFIG_INFO_NULL(AppRspCodeConstant.AREA_CODE_CONFIG_INFO_NULL, "区号配置为空"),
    SMS_TEMPLATE_INFO_NULL(AppRspCodeConstant.SMS_TEMPLATE_INFO_NULL, "短信模板为空"),
    SMS_CONFIG_INFO_NULL(AppRspCodeConstant.SMS_CONFIG_INFO_NULL, "发送短信配置为空"),
    SMS_SEND_ERROR(AppRspCodeConstant.SMS_SEND_ERROR, "短信发送失败"),
    SMS_ALREADY_SEND(AppRspCodeConstant.SMS_ALREADY_SEND, "短信已经发送"),
    SMS_ONE_MINUTE_AFTER_TRY_AGAIN(AppRspCodeConstant.SMS_ONE_MINUTE_AFTER_TRY_AGAIN, "一分钟之后再试"),
    SMS_SEND_NUM_MAX(AppRspCodeConstant.SMS_SEND_NUM_MAX, "达到每次最大发送次数"),


    /**
     * 扩展
     */
    SEND_SMS_CODE_ERROR(AppRspCodeConstant.SEND_SMS_CODE_ERROR, "短信验证码发送失败"),

    ;


    private String code;
    private String message;
    private String[] params;

    AppRspCodeEnum(String code, String message, String... params) {
        this.code = code;
        this.message = message;
        this.params = params;
    }

    AppRspCodeEnum(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getMessage(String... params) {
        return String.format(message, params);
    }

    private static Map<String, String> rspCodeMap = new HashMap<String, String>(10);
    private static Map<String, AppRspCodeEnum> rspCodeEnumMap = new HashMap<String, AppRspCodeEnum>(10);

    /**
     * 获取返回的枚举类型
     *
     * @param code
     * @return
     */
    public static AppRspCodeEnum getRspCodeEnum(String code) {
        return rspCodeEnumMap.get(code);
    }

    /**
     * 获取返回信息
     *
     * @param code
     * @return
     */
    public static String getRspMsg(String code) {
        return rspCodeMap.get(code);
    }

    /**
     * 获取返回信息 ,带参数
     *
     * @param code
     * @return
     */
    public static String getRspCodeEnum(String code, String... params) {
        return rspCodeEnumMap.get(code).getMessage(params);
    }

    static {
        AppRspCodeEnum[] appRspCodeEnumArry = AppRspCodeEnum.values();
        for (AppRspCodeEnum appRspCodeEnum : appRspCodeEnumArry) {
            rspCodeMap.put(appRspCodeEnum.getCode(), appRspCodeEnum.getMessage());
            rspCodeEnumMap.put(appRspCodeEnum.getCode(), appRspCodeEnum);
        }
    }
}
